package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class GroupHelper extends HelperBase{
		
	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<GroupData> cachedGroups;
	
	public SortedListOf<GroupData> getGroups() {
		return getGroups(ENABLESORT);
	}
	
	public SortedListOf<GroupData> getGroups(Boolean doSort) {
		if(cachedGroups == null) {
			rebuildCache(doSort); 
		}
		return cachedGroups;
	}
	
	private void rebuildCache() {
		rebuildCache(ENABLESORT);
	}
	
	private void rebuildCache(Boolean doSort) {
		boolean sortInEachIteration = false;
		manager.navigateTo().groupsPage();		
		
		cachedGroups = new SortedListOf<GroupData>();
		List<WebElement> checkboxes = findElements(By.xpath(".//input[@name='selected[]']"));
		for (int i = 0; i < checkboxes.size(); i++) {
			if (i == checkboxes.size()-1){
				sortInEachIteration = doSort;
			}
			WebElement checkbox = checkboxes.get(i);
			String title = checkbox.getAttribute("title");			
			String name = title.substring("Select (".length(), title.length()-1);
			cachedGroups.add(new GroupData().withName(name),sortInEachIteration);
		}		
	}

	public GroupHelper createGroup(GroupData group) {
		manager.navigateTo().groupsPage();
		
		initGroupCreation();
		fillGroupForm(group);
		submitGroupCreation();
		returnToGroupsPage();	
		rebuildCache();
		return this;
	}
	
	public GroupHelper modifyGroup(int index, GroupData group) {
		manager.navigateTo().groupsPage();
		
		initGroupModification(index);
		fillGroupForm(group);
		submitGroupModification();   
		returnToGroupsPage();	 
		rebuildCache();
		return this;		
	}
	
	public GroupHelper deleteGroup(int index) {
		manager.navigateTo().groupsPage();
		selectGroupByIndex(index);
		submitGroupDeletion();			
		returnToGroupsPage();
		rebuildCache();
		return this;
	}
// ----------------------------------------------------------------------------

	private void submitGroupDeletion() {
		click(By.name("delete"));
		cachedGroups = null;
	}
	
	public GroupHelper initGroupCreation() {
		click(By.name("new"));
		return this;
	}

	public GroupHelper fillGroupForm(GroupData group) {
		type(By.name("group_name"), group.getName());
		type(By.name("group_header"), group.getHeader());
		type(By.name("group_footer"), group.getFooter());
		return this;
	}

	public GroupHelper submitGroupCreation() {
	    click(By.name("submit"));
	    cachedGroups = null;
	    return this;
	}

	public GroupHelper returnToGroupsPage() {
	    click(By.linkText("group page"));
	    return this;
	}

	private void selectGroupByIndex(int index) {
		click(By.xpath("//input[@name='selected[]'][" + (index+1) + "]"));
	}

	public GroupHelper initGroupModification(int index) {
		selectGroupByIndex(index);
		click(By.name("edit"));			
		return this;
	}

	public GroupHelper submitGroupModification() {
		click(By.name("update"));	
		cachedGroups = null;
		return this;
	}


}
